package com.m3958.firstgwt.server.dao;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.persistence.Query;

import com.google.inject.Inject;
import com.m3958.firstgwt.server.model.Article;
import com.m3958.firstgwt.server.model.BaseModel;


public class ArticleDao  extends BaseDao<Article>{
	
	@Inject
	public ArticleDao(ArticleChangeStrategy task) {
		super(Article.class);
		extraStrategyTask = task;
	}
	
	
	
	@Override
	public Integer smartCustomCount() {
		return null;
	}

	@Override
	public List<BaseModel> smartCustomFetch() {
		return null;
	}

//	public final static String NAMED_QUERY_NAME = "_queryName";
//	FIND_ARTICLES_BY_TAGNAMES,siteId,tags,ta,tb,tc,_startRow,_endRow
// 	http://www.m3958.com/smartcfud?_jsonp=true&_modelName=com.m3958.firstgwt.server.model.Article&_operationType=fetch&_sortBy=-publishedAt&_subOperationType=NAMED_QUERY&_jsonv=v&_queryName=findArticlesByTagNames&siteId=61517&_endRow=5&_startRow=0&tags=%E9%A6%96%E9%A1%B5%E5%B9%BB%E7%81%AF&callback=YUI.Env.DataSource.callbacks.yui_3_5_1_2_1340805145849_92
	@Override
	public List<Article> smartNamedQueryFetch() {
		if(Article.NamedQueries.FIND_ARTICLES_BY_TAGNAMES_HAS_TITLEIMG.equals(getReqPs().getNamedQueryName())){
			int siteId = getReqPs().getIdValue("siteId");
			String tagsStr = getReqPs().getStringValue("tags","");
			String[] tags = tagsStr.split(",");
			if(tags.length == 0)return null;
			int start = getReqPs().getIntegerValue("_startRow");
			int end = getReqPs().getIntegerValue("_endRow");
			Query q = emp.get().createNamedQuery(Article.NamedQueries.FIND_ARTICLES_BY_TAGNAMES_HAS_TITLEIMG);
			q.setParameter("siteId", siteId);
			q.setParameter("audit", true);
			q.setParameter("tagNames", Arrays.asList(tags));
			q.setMaxResults(end - start);
			List<Article> ars =  q.getResultList();
			return ars;
		}else if(Article.NamedQueries.FIND_ARTICLES_BY_TAGNAMES.equals(getReqPs().getNamedQueryName())){
			int siteId = getReqPs().getIdValue("siteId");
			String tagsStr = getReqPs().getStringValue("tags","");
			String[] tags = tagsStr.split(",");
			if(tags.length == 0)return null;
			int start = getReqPs().getIntegerValue("_startRow");
			int end = getReqPs().getIntegerValue("_endRow");
			Query q = emp.get().createNamedQuery(Article.NamedQueries.FIND_ARTICLES_BY_TAGNAMES);
			q.setParameter("siteId", siteId);
			q.setParameter("audit", true);
			q.setParameter("tagNames", Arrays.asList(tags));
			q.setMaxResults(end - start);
			List<Article> ars =  q.getResultList();
			return ars;
		}else if(Article.NamedQueries.FIND_SITE_MOST_RECENTLY_ARTICLES.equals(getReqPs().getNamedQueryName())){
			int siteId = getReqPs().getIdValue("siteId");
			int start = getReqPs().getIntegerValue("_startRow");
			int end = getReqPs().getIntegerValue("_endRow");
			Query q = emp.get().createNamedQuery(Article.NamedQueries.FIND_SITE_MOST_RECENTLY_ARTICLES);
			q.setParameter("siteId", siteId);
			q.setParameter("audit", true);
			q.setMaxResults(end - start);
			List<Article> ars =  q.getResultList();
			return ars;
		}else if(Article.NamedQueries.GET_ARTICLES_BY_IDS.equals(getReqPs().getNamedQueryName())){
			String ids = getReqPs().getStringValue("ids");
			String[] idss = ids.split(",");
			List<Integer> iids = new ArrayList<Integer>();
			for(String id:idss){
				iids.add(Integer.valueOf(id));
			}
			Query q = emp.get().createNamedQuery(Article.NamedQueries.GET_ARTICLES_BY_IDS);
			q.setParameter("idlist", iids);
			q.setParameter("audit", true);
			List<Article> ars =  q.getResultList();
			return ars;
		}
		return null;
	}

	@Override
	public Integer smartNamedQueryFetchCount() {
		if(Article.NamedQueries.FIND_ARTICLES_BY_TAGNAMES_HAS_TITLEIMG.equals(getReqPs().getNamedQueryName())){
			int siteId = getReqPs().getIdValue("siteId");
			String tagsStr = getReqPs().getStringValue("tags","");
			String[] tags = tagsStr.split(",");
			if(tags.length == 0)return null;
			Query q = emp.get().createNamedQuery(Article.NamedQueries.FIND_ARTICLES_BY_TAGNAMES_HAS_TITLEIMG_COUNT);
			q.setParameter("siteId", siteId);
			q.setParameter("audit", true);
			q.setParameter("tagNames", Arrays.asList(tags));
			Long n =  (Long) q.getSingleResult();
			return n.intValue();
		}else if(Article.NamedQueries.FIND_ARTICLES_BY_TAGNAMES.equals(getReqPs().getNamedQueryName())){
			int siteId = getReqPs().getIdValue("siteId");
			String tagsStr = getReqPs().getStringValue("tags","");
			String[] tags = tagsStr.split(",");
			if(tags.length == 0)return null;
			Query q = emp.get().createNamedQuery(Article.NamedQueries.FIND_ARTICLES_BY_TAGNAMES_COUNT);
			q.setParameter("siteId", siteId);
			q.setParameter("audit", true);
			q.setParameter("tagNames", Arrays.asList(tags));
			Long n =  (Long) q.getSingleResult();
			return n.intValue();
		}
		return null;
	}

}
